package org.apache.spark.examples.es;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import org.elasticsearch.spark.rdd.api.java.JavaEsSpark;
import org.spark_project.guava.collect.ImmutableList;
import org.spark_project.guava.collect.ImmutableMap;

import java.util.Map;

/**
 * Created by admin on 2019/3/28.
 */
public class JavaESearchExample {

    public static void main(String[] args){
        System.setProperty("hadoop.home.dir", "C:/hadoop-2.6.0");
        SparkConf conf = new SparkConf().setAppName("JavaESearchExample").setMaster("local[1]");
        conf.set("es.nodes", "172.30.17.156");
        conf.set("es.port","9200");
        conf.set("es.index.auto.create", "true");
        conf.set("es.write.operation", "index");
        conf.set("es.index.auto.create", "true");
        JavaSparkContext  jsc = new JavaSparkContext(conf);

        Map<String, ?> numbers = ImmutableMap.of("one", 1, "two", 2);
        Map<String, ?> airports = ImmutableMap.of("OTP", "Otopeni", "SFO", "San Fran");
        JavaRDD<Map<String, ?>> javaRDD = jsc.parallelize(ImmutableList.of(numbers, airports));
        //第一个dataframe 第二个形参格式 _index/_type
        javaRDD.foreach(new VoidFunction<Map<String, ?>>() {
            @Override
            public void call(Map<String, ?> stringMap) throws Exception {
                for (Map.Entry<String, ?> entry : stringMap.entrySet()) {
                    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                }
            }
        });

        JavaEsSpark.saveToEs(javaRDD, "spark/docs"); //index/type



    }
}
